﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenStudio.IDE.Core.ConsoleLibrary
{
        public static class CMS
    {
        #region Public Members
        /// <summary>
        /// 
        /// </summary>
        public static System.ConsoleColor LBGColor;
        /// <summary>
        /// 
        /// </summary>
        public static System.ConsoleColor LFGColor;
        /// <summary>
        /// 
        /// </summary>
        public static int LastBufferWidth;
        /// <summary>
        /// 
        /// </summary>
        public static int LastBufferHeight;
        /// <summary>
        /// 
        /// </summary>
        public static int LastWindowWidth;
        /// <summary>
        /// 
        /// </summary>
        public static int LastWindowHeight;
        #endregion
        #region Color Related
        /// <summary>
        /// Set the new Background Color
        /// </summary>
        /// <param name="NewColor">System.ConsoleColor: New Background Color</param>
        public static void SetBackgroundColor(System.ConsoleColor NewColor)
        {
            // Backup Current BackgroundColor
            LBGColor = System.Console.BackgroundColor;
            System.Console.BackgroundColor = NewColor;
        }
        /// <summary>
        /// Set the new Foreground Color
        /// </summary>
        /// <param name="NewColor">System.ConsoleColor: New Foreground Color</param>
        public static void SetForegroundColor(System.ConsoleColor NewColor)
        {
            // Backup Current ForegroundColor
            LFGColor = System.Console.ForegroundColor;
            System.Console.ForegroundColor = NewColor;
        }
        /// <summary>
        /// Set The Foreground Color and the Background Color
        /// </summary>
        /// <param name="BackgroundColor">System.ConsoleColor: New Background Color</param>
        /// <param name="ForegroundColor">System.ConsoleColor: New Foreground Color</param>
        public static void SetColors(System.ConsoleColor BackgroundColor, System.ConsoleColor ForegroundColor)
        {
            SetBackgroundColor(BackgroundColor);
            SetForegroundColor(ForegroundColor);
        }
        /// <summary>
        /// Restors the Background Color that wer used befor the last color change
        /// </summary>
        public static void RestoreBackgroundColor()
        {
            System.ConsoleColor tmp = System.Console.BackgroundColor;
            System.Console.BackgroundColor = LBGColor;
            LBGColor = tmp;
        }
        /// <summary>
        /// Restors the Foreground Color that wer used befor the last color change
        /// </summary>
        public static void RestoreForegroundColor()
        {
            System.ConsoleColor tmp = System.Console.ForegroundColor;
            System.Console.ForegroundColor = LFGColor;
            LFGColor = tmp;
        }
        /// <summary>
        /// Restors the Foreground Color and the Background Color that wer used befor the last color change
        /// </summary>
        public static void RestoreColors()
        {
            RestoreBackgroundColor();
            RestoreForegroundColor();
        }
        #endregion
        #region Buffer Related
        /// <summary>
        /// Set the console Buffer width
        /// </summary>
        /// <param name="NewWidth">System.Int32: New Buffer Width</param>
        public static void SetBufferWidth(int NewWidth)
        {
            LastBufferWidth = System.Console.BufferWidth;
            System.Console.BufferWidth = NewWidth;
        }
        /// <summary>
        /// Set the console buffer height
        /// </summary>
        /// <param name="NewHeight">System.Int32: New Buffer Height</param>
        public static void SetBufferHeight(int NewHeight)
        {
            LastBufferHeight = System.Console.BufferHeight;
            System.Console.BufferHeight = NewHeight;
        }
        /// <summary>
        /// Set the console buffer height and buffer width
        /// </summary>
        /// <param name="NewWidth">System.Int32: new buffer width</param>
        /// <param name="NewHeight">System.Int32: new buffer Height</param>
        public static void SetBuffers(int NewWidth, int NewHeight)
        {
            SetBufferWidth(NewWidth);
            SetBufferHeight(NewHeight);
        }
        /// <summary>
        /// Restors the buffer width that was used before the last change
        /// </summary>
        public static void RestoreBufferWidth()
        {
            int tmp = System.Console.BufferWidth;
            System.Console.BufferWidth = LastBufferWidth;
            LastBufferWidth = tmp;
        }
        /// <summary>
        /// Restors the buffer height that was used before the last change
        /// </summary>
        public static void RestoreBufferHeight()
        {
            int tmp = System.Console.BufferHeight;
            System.Console.BufferHeight = LastBufferHeight;
            LastBufferHeight = tmp;
        }
        /// <summary>
        /// Restors the buffer width and height that was used before the last change
        /// </summary>
        public static void RestoreBuffers()
        {
            RestoreBufferWidth();
            RestoreBufferHeight();
        }
        #endregion
        #region Window Size Related
        /// <summary>
        /// Set the window width
        /// </summary>
        /// <param name="NewWidth">System.Int32: new width</param>
        public static void SetWindowWidth(int NewWidth)
        {
            LastWindowWidth = System.Console.WindowWidth;
            System.Console.WindowWidth = NewWidth;
        }
        /// <summary>
        /// Set the window height
        /// </summary>
        /// <param name="NewHeight">System.Int32: new height</param>
        public static void SetWindowHeight(int NewHeight)
        {
            LastWindowHeight = System.Console.WindowHeight;
            try
            {
                System.Console.WindowHeight = NewHeight;
            }
            catch (Exception) { }
        }
        /// <summary>
        /// Set the Window Width and height
        /// </summary>
        /// <param name="NewWidth">System.Int32: new Width</param>
        /// <param name="NewHeight">System.Int32: new Height</param>
        public static void SetWindow(int NewWidth, int NewHeight)
        {
            SetWindowWidth(NewWidth);
            SetWindowHeight(NewHeight);
        }
        /// <summary>
        /// Restors the window width that was lat used befor the last change
        /// </summary>
        public static void RestoreWindowWidth()
        {
            int tmp = System.Console.WindowWidth;
            System.Console.WindowWidth = LastWindowWidth;
            LastWindowWidth = tmp;
        }
        /// <summary>
        /// Restors the window height that was lat used befor the last change
        /// </summary>
        public static void RestoreWindowHeight()
        {
            int tmp = System.Console.WindowHeight;
            System.Console.WindowHeight = LastWindowHeight;
            LastWindowHeight = tmp;
        }
        /// <summary>
        /// Restors the window height and width that was lat used befor the last change
        /// </summary>
        public static void RestoreWindow()
        {
            RestoreWindowWidth();
            RestoreWindowHeight();
        }
        #endregion
        #region System.Console I/O
        /// <summary>
        /// Fill a line with a background color after text.
        /// </summary>
        /// <param name="Line">Line of text to be written.</param>
        public static void WriteFill(string Line)
        {
            System.Console.WriteLine(Line);
        }
        /// <summary>
        /// Fill a line with(no EOL). background color after text.
        /// </summary>
        /// <param name="Line">Line of text to be written</param>
        public static void WriteFillnb(string Line)
        {
            System.Console.Write(Line);
        }
        /// <summary>
        /// Writes Exception data to the System.Console window on multiple lines.
        /// </summary>
        /// <param name="Ex">Exception to be formatted.</param>
        public static void WriteException(Exception Ex)
        {
            WriteError("[Managed Exception Occured]");
            WriteError("Message: " + Ex.Message);
            WriteError("Source: " + Ex.Source);
            WriteError("StackTrace: " + Ex.StackTrace);
            WriteError("TargetSite: " + Ex.TargetSite);
            WriteError("InnerException: " + Ex.InnerException);
            WriteError("TargetSite.Name: " + Ex.TargetSite.Name);
            WriteError("TargetSite.MemberType: " + Ex.TargetSite.MemberType.ToString());
            WriteError("Data: " + Ex.Data);
        }
        /// <summary>
        /// Writes a line of error test to the System.Console window.
        /// </summary>
        /// <param name="Line">Error text line to be written.</param>
        public static void WriteError(string Line)
        {
            SetColors(System.ConsoleColor.Red, System.ConsoleColor.Black);
            WriteFill(Line);
            RestoreColors();
        }
        /// <summary>
        /// Write's a line of debug text to the System.Console window.
        /// </summary>
        /// <param name="Line">Debug text line to be written.</param>
        public static void WriteDebug(string Line)
        {
            SetColors(System.ConsoleColor.Black, System.ConsoleColor.DarkGray);
            System.Console.WriteLine(Line);
            RestoreColors();
        }
        /// <summary>
        /// Write a line of text to the System.Console window.
        /// </summary>
        /// <param name="Line">Line of text to be written.</param>
        public static void WriteLine(string Line)
        {
            System.Console.WriteLine(Line);
        }
        /// <summary>
        /// Write a line of text to the System.Console window. with a forground color
        /// </summary>
        /// <param name="Line">Line of text to be written.</param>
        /// <param name="textcolro">Color of the text</param>
        public static void WriteLine(string Line, System.ConsoleColor textcolro)
        {
            SetColors(System.ConsoleColor.Black, textcolro);
            WriteFill(Line);
            RestoreColors();
        }
        /// <summary>
        /// Write text to the System.Console window (no EOL).
        /// </summary>
        /// <param name="Line">Text to be written.</param>
        public static void Write(string Line)
        {
            System.Console.Write(Line);
        }
        /// <summary>
        /// Write text to the System.Console window (no EOL). and a text color 
        /// </summary>
        /// <param name="Line">Text to be written.</param>
        /// <param name="textcolro">the text color</param>
        public static void Write(string Line, System.ConsoleColor textcolro)
        {
            SetColors(System.ConsoleColor.Black, textcolro);
            WriteFillnb(Line);
            RestoreColors();
        }
        /// <summary>
        /// Read a line of text from the System.Console window input.
        /// </summary>
        /// <returns>Text read from the StandardInput.</returns>
        public static string ReadLine()
        {
            return System.Console.ReadLine();
        }
        #endregion
        #region "Other"
        /// <summary>
        /// Clears the System.Console Window
        /// </summary>
        public static void Clear()
        {
            System.Console.Clear();
        }
        /// <summary>
        /// Sets or gets the System.Console window title
        /// </summary>
        public static string Title
        {
            get { return System.Console.Title; }
            set { System.Console.Title = value; }
        }
        #endregion
    }
